<?php



abstract class AbstractDAO {

    /**
     * database connection instance
     * @var type DB
     */
    protected $db;

    /**
     * Constructor
     */
    //public function AbstractDAO( $dbInstance ) {
    //    $this->db = $dbInstance;
    //}
    
    public function __construct()
    {
    	$this->db = GetDB();
    	
    }

    /**
     * get limited result set by page
     * @param string $sql
     * @param int $perpage
     * @param int $cpage current page
     * @return array
     */
    protected function getRows($sql,$perpage,$cpage=1,$function="GetPlan") {
        $start = ($cpage-1) * $perpage;
        $end = $perpage;

        $sql .= " LIMIT $start,$end";

        //print $sql;
        //die();
        $result = $this->db->$function($sql);
        return $result;
    }

	protected function& SortByIdArray( $needSortArray, $idArray, $sortBy = 'id' )
	{
		$result = array();
		foreach( $idArray as $key => $value )
			$result[] = $this->GetItemFromArray( $needSortArray, $sortBy, $value );
			
		return $result;
	}
	
	protected function& GetItemFromArray( &$array, $k, $compareValue )
	{
		$n = null;
		foreach( $array as $key => $value ){
			if( $value[$k] == $compareValue )
				return $value;
		}
		
		return $n;
	}
}
?>